package org.yuxiong.utils.mail;

import java.io.IOException;
import java.util.Properties;

import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

public class Test {

    public static void main(String[] args) {
      /*  try {
            for (int i = 1; i < 2; i++) {
                SendMail sm = new SendMail();
                sm.setSmtpHost("smtp.sohu.com");
                sm.setMailFrom("yuxiong023@sohu.com");
                sm.setMailTo("37265773@qq.com");
                sm.setMsgContent("支持奥运与北京同行");
                sm.setSubject("奥运");
                int num = sm.sendMail();
                System.out.println(num);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }*/
    	
    	String host = "pop3.sohu.com";
    	String username = "yuxiong023";
    	String password = "791216";

    	// Create empty properties
    	Properties props = new Properties();

    	// Get session
    	Session session = Session.getDefaultInstance(props, null);

    	// Get the store
    	Store store;
		try {
			store = session.getStore("pop3");
			store.connect(host, username, password);

	    	// Get folder
	    	Folder folder = store.getFolder("INBOX");
	    	folder.open(Folder.READ_ONLY);
	    	
	    	Message[] messages = folder.getMessages();
	    	FetchProfile fp = new FetchProfile();
	    	fp.add(FetchProfile.Item.ENVELOPE);
	    	fp.add(FetchProfile.Item.FLAGS);
	    	fp.add("X-Mailer");
	    	folder.fetch(messages, fp);
	    	
	    	
	    	for (int i=0; i<messages.length; i++) {
	    		System.out.println(i + ": " + messages[i].getFrom()[0] 
                   + "\t" + messages[i].getSubject() + "\t" + messages[i].getContent().toString());
	    	}
	    	
	    	// Close connection
	    	folder.close(false);
	    	store.close();
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	
    }
}
